Skip to content

fix(oci): keep streamed parallel tool calls separate and in order - #8048

Closed
gjija wants to merge 1 commit into
google:mainfrom
gjija:fix/oci-stream-parallel-tool-calls
Closed

gjija wants to merge 1 commit into
google:mainfrom
gjija:fix/oci-stream-parallel-tool-calls

Conversation

@gjija

@gjija gjija commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

2. Or, if no issue exists, describe the change:

Problem:

OCIGenAILlm streaming keys each tool call with tc.get("index", tc_idx). OCI tool calls carry no index (the SDK's FunctionCall has only id, type, name and arguments), and each chunk holds one call, so every call lands in slot 0. When the model calls two tools in one turn, the second call overwrites the first call's id and name and its arguments are appended to the first call's. json.loads then fails, and the final response holds one call with {} as args: the first call is lost and the second runs with no arguments.

The final calls were also sorted by name instead of kept in the order they arrived.

Two calls in separate chunks, the shape _make_sse_chunks already builds:

expected: [('call_1', 'get_weather', {'city': 'Paris'}), ('call_2', 'get_time', {'tz': 'CET'})]
on main:  [('call_2', 'get_time', {})]

Solution:

Group the pieces by index when one is sent, else by id. A piece with a name but no id starts a new call, and a piece with neither continues the last call. This is the rule ApigeeLlm uses for tool-call chunks without an index (#7645). The calls now come out in arrival order, like the non-streaming path. One call split over several chunks (same index, same id, or the id only on the first piece) still merges into one call.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

New tests in tests/unittests/integrations/oci/test_oci_genai_llm.py:

  • test_streaming_parallel_tool_calls_in_separate_chunks, with ids and with empty ids
  • test_streaming_tool_calls_split_across_chunks, for same_index, same_id and id_first_only

All 5 fail on main (5 failed, 56 passed) and pass with the fix:

$ pytest tests/unittests/integrations/oci
61 passed in 0.73s

pyink and isort leave both files unchanged.

Manual End-to-End (E2E) Tests:

I don't have an OCI account, so I could not run this against a live OCI endpoint, and tests/integration/integrations/oci skips without OCI_COMPARTMENT_ID. The unit tests above stand in for E2E: they feed generate_content_async(stream=True) the chunk shape from _make_sse_chunks, whose docstring says it was checked against a live OCI Gemini stream, plus the split shapes.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

The order change is part of the same fix: once the calls are kept apart, sorting them by name would still reorder a turn's calls, which the non-streaming path does not do.

The OCI streaming path keyed each tool call by its "index", falling back
to its position in the chunk. OCI tool calls carry no "index" and each
chunk holds one call, so every call landed in slot 0: a second call
overwrote the first call's id and name and appended its arguments,
json.loads failed, and the response held one call with empty args. The
calls were also sorted by name instead of kept in arrival order.

Group pieces by index when one is sent, else by id. A piece with a name
but no id starts a new call, and a piece with neither continues the last
call, the same rule ApigeeLlm uses. Calls now keep the order they
arrived in, like the non-streaming path.
copybara-service Bot pushed a commit that referenced this pull request Nov 10, 2026
Merge #8048

OCIGenAILlm streaming now groups tool-call pieces by index, else by id, and
returns the calls in arrival order instead of sorted by name.

PiperOrigin-RevId: 1018625954
@adk-bot

adk-bot commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

Thank you @gjija for your contribution! 🎉

Your changes have been successfully imported and merged via Copybara in commit 353a771.

Closing this PR as the changes are now in the main branch.

@adk-bot adk-bot added the merged [Status] This PR is merged label Nov 10, 2026
@adk-bot adk-bot closed this Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged [Status] This PR is merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants